package com.mycjj.android.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.ReservationBaseBean;
import com.mycjj.android.bean.ReservationEntity;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class YuyueActivity extends BaseActivity implements android.view.View.OnClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_yuyue);
		init();
	}
	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("预约");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				YuyueActivity.this.finish();
			}
		});
	}
	private ListView listview_yuyue;
	private YuyueAdapter yuyueAdapter;
	private TextView mTv_yuyue_add;
	private void init(){
		initHeader();
		mTv_yuyue_add = (TextView)findViewById(R.id.tv_yuyue_add);
		mTv_yuyue_add.setOnClickListener(this);
		listview_yuyue = (ListView)findViewById(R.id.lv_yuyue);
		yuyueAdapter = new YuyueAdapter(this);
		listview_yuyue.setAdapter(yuyueAdapter);
		listview_yuyue.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
					Intent intent = new Intent(YuyueActivity.this,YuyueDetailActivity.class);
					intent.putExtra("id", String.valueOf(entityList.get(position).getId()));
					startActivity(intent);
			}
			
		});
	}
	
	private List<ReservationEntity> entityList;
	private void initData(){
		request = new ProjectHttpRequestImpl(this, true);
		
		String token = Preferences.getSharedPreferences(this, "user_token", "");
		Log.i("info", "======"+token);
		RequestParams params = new RequestParams();
		params.put("page", "0");
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		
		params.put("token", token);
		
		request.requestReservationGetsWithDoneHandler("",params,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				ReservationBaseBean bean = ProjectDataManage.getInstance().getReservationBaseBean();
				if(bean != null){
					Log.e("mark","bean:"+bean.toString());
					if(bean.getCode() == 200){
						if(bean.getData() != null){
							
							entityList = bean.getData().getList(); 
							if(entityList != null && entityList.size() > 0){
								yuyueAdapter.setList(entityList);
							}else{
								Log.e("mark", "list is empty!!!");
							}
						}else{
							Log.e("mark","T(ReservationBean) is null");
						}
					}else{
						Toast.makeText(YuyueActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
					}
				}else {
					Toast.makeText(YuyueActivity.this, "bean is empty!", Toast.LENGTH_LONG).show();
					Log.e("mark","bean is empty");
				}
			}
		});
	}
	
	public class YuyueAdapter extends TAdapter<ReservationEntity>{

		public YuyueAdapter(Context mContext) {
			super(mContext);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_yuyue_view, null);
				holder = new ViewHolder();
				holder.tv_yy_type = (TextView) convertView.findViewById(R.id.tv_yy_type);
				holder.tv_title = (TextView)convertView.findViewById(R.id.tv_yy_title);
				holder.tv_desc = (TextView)convertView.findViewById(R.id.tv_yy_desc);
				holder.tv_yy_state = (TextView)convertView.findViewById(R.id.tv_yy_state);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			ReservationEntity entity = mList.get(position);
			holder.tv_title.setText(entity.getStore_name());
			holder.tv_desc.setText(entity.getArrive_date());//预约时间
			int if_cancel = entity.getIf_cancel();
			String state = "未受理";
			switch(if_cancel){
			case 1:
				state = "已取消";
				holder.tv_yy_state.setBackgroundColor(Color.parseColor("#1E90FF"));
				break;
			case 0:
			default:
				switch(entity.getStatus()){
				case 0:
					state = "未受理";//红
					holder.tv_yy_state.setBackgroundColor(Color.parseColor("#e83710"));
					break;
				case 1:
					state = "已受理";//绿
					holder.tv_yy_state.setBackgroundColor(Color.parseColor("#1f9a25"));
					break;
				case 2:
					state = "待评价";//橙
					holder.tv_yy_state.setBackgroundColor(Color.parseColor("#e39611"));
					break;
				case 3:
					state = "已完成";//灰
					holder.tv_yy_state.setBackgroundColor(Color.parseColor("#999999"));
					break;
				default:
					state = "未受理";//红
					holder.tv_yy_state.setBackgroundColor(Color.parseColor("#e83710"));
					break;
				}
				break;
			}
			holder.tv_yy_state.setText(state);
			
			return convertView;
		}
		
		class ViewHolder{
			TextView tv_yy_type;
			TextView tv_title;
			TextView tv_desc;
			TextView tv_yy_state;
		}
		
		

	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tv_yuyue_add:{
//			getPlanState("6", "2014-06");
			Intent intent = new Intent(this,CarListActivity.class);
			startActivity(intent);
		}
			break;
		}
	}
	
}
